package com.example.service.impl;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.example.dao.StudentDAO;
import com.example.entity.StudentEntity;
import com.example.param.StudentRequest;
import com.example.service.StudentService;
import com.example.vo.StudentVO;

import ch.qos.logback.core.status.Status;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentDAO studentDAO;
	
	@Autowired
	private RedisTemplate<String,Object> redisTemplate;

	
	private static final String KEY = "redis_";
	
	@Override
	public int addStudent(StudentRequest request) {
		if (request == null) {
			return Status.ERROR;
		}
		StudentEntity entity = new StudentEntity();
		entity.setId(request.getId());
		entity.setName(request.getName());
		entity.setBirthday(request.getBirthday());
		entity.setGender(request.getGender());
		entity.setPhone(request.getPhone());
		int result = studentDAO.add(entity);
		if (result!=1) {
			return Status.ERROR;
		}
		return Status.INFO;
	}

	@Override
	public int deleteStudent(Set<Integer> ids) {
		if (ids == null) {
		return Status.ERROR;
		}
		return studentDAO.delete(ids);
	}

	@Override
	public int updateStudent(StudentRequest request) throws RuntimeException{
		if (request == null) {
			return Status.ERROR;
		}
		StudentEntity entity = new StudentEntity();
		entity.setId(request.getId());
		entity.setName(request.getName());
		entity.setBirthday(request.getBirthday());
		entity.setGender(request.getGender());
		entity.setPhone(request.getPhone());
		return studentDAO.update(entity);
	}

	@Override
	public StudentVO queryById(Integer id) {
		if (id == null) {
			throw new NullPointerException();
		}
		StudentEntity entity = studentDAO.queryById(id);
		if (entity == null) {
			throw new NullPointerException();
		}
//		StudentVO studentVO = new StudentVO();
//		studentVO.
//		studentVO.setName(entity.getName());
//		studentVO.setBirthday(entity.getBirthday());
//		studentVO.setGender(entity.getGender());
//		studentVO.setPhone(entity.getPhone());
		
	
		String jsonString=JSON.toJSONString(entity, false);
		redisTemplate.boundValueOps(KEY+id).set(jsonString);
		 redisTemplate.expire(KEY+id, 1, TimeUnit.DAYS);
		 Object object1=redisTemplate.boundValueOps(KEY+id).get();
		 System.out.println(object1);
		return null;
	}

//	@Override
//	public StudentVO queryAll() {
//		StudentEntity entity = studentDAO.queryAll();
//		StudentVO studentVO = new StudentVO();
//		studentVO.setName(entity.getName());
//		studentVO.setBirthday(entity.getBirthday());
//		studentVO.setGender(entity.getGender());
//		studentVO.setPhone(entity.getPhone());
//		this.redisTemplate.boundSetOps(studentVO.getId())
//		return null;
//	}

}
